<?php

namespace App\Http\Controllers\Admin\Setting\Permission;

use App\Http\Controllers\Admin\Controller;
use App\Repositories\Permission\PermissionRepository;
use App\Repositories\Permission\RoleRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Inertia\Response;

class RoleController extends Controller
{
    protected array $validateMessages = [
        'slug.*' => '请输入角色标识',
        'name.*' => '请输入角色名称',
        'description.*' => '请输入角色描述',
        'status.*' => '请输入正确的状态',
        'sort.*' => '请输入正确的排序',
    ];

    public function __construct(Request $request, RoleRepository $repository)
    {
        $this->request = $request;
        $this->repository = $repository;
    }
    public function index( PermissionRepository $permissionRepository): Response
    {
        $list = $this->repository->paginate();
        return $this->inertia('setting/permission/role/index',[
            'data' => $list->items(),
            'pagination' => [
                'total' => $list->total(),
                'page' => $list->currentPage(),
                'pageSize' => $list->perPage(),
                'showTotal' => true,
            ],
            'options' => $permissionRepository->getSelect(),
        ]);
    }

    public function show($id): array
    {
        $row = $this->repository->getById($id);
        return [
            'data' => [
                'id' => $row->id,
                'name' => $row->name,
                'slug' => $row->slug,
                'description' => $row->description,
                'status' => $row->status,
                'sort' => $row->sort,
                'rules' => $row->getPermissionIds(),
            ],
        ];
    }

    /**
     * @throws ValidationException
     */
    public function store(): JsonResponse
    {
        $params = $this->validate(
            $this->request,
            [
                'name' => 'required|string',
                'slug' => 'required|string',
                'status' => 'required|integer',
                'sort' => 'required|integer',
                'description' => 'required|string',
            ],
            $this->validateMessages
        );
        $row = $this->repository->create($params);
        $rules = $this->request->input('rules', []);
        $row->assignPermissions($rules);
        return $this->responseCreated();
    }

    /**
     * @throws ValidationException
     */
    public function update($id): JsonResponse
    {
        $params = $this->validate(
            $this->request,
            [
                'name' => 'required|string',
                'slug' => 'required|string',
                'status' => 'required|integer',
                'sort' => 'required|integer',
                'description' => 'required|string',
            ],
            $this->validateMessages
        );
        $row = $this->repository->getById($id);
        $row->update($params);
        $rules = $this->request->input('rules', []);
        $row->assignPermissions($rules);
        return $this->responseSuccess();
    }

    public function destroy($id): JsonResponse
    {
        $row = $this->repository->getById($id);
        $row->delete();
        $row->permissions()->detach();
        return $this->responseDeleted();
    }
}
